"""
Pydantic 错误消息汇总
"""

from typing import (
    Dict,
    List,
    Type,
)

from pydantic_x.errors.base import PydanticErrorBase
from pydantic_x.version import IS_PYDANTIC_V1

if IS_PYDANTIC_V1:
    from pydantic_x.errors.v1_errors import (
        PydanticV1BuiltInError as PydanticBuiltInError,
        PydanticV1CustomErrorBase as PydanticCustomErrorBase,
    )

else:
    from pydantic_x.errors.v2_errors import (
        PydanticV2BuiltInError as PydanticBuiltInError,
        PydanticV2CustomErrorBase as PydanticCustomErrorBase,
    )


def get_code_2_error() -> Dict[str, Type[PydanticErrorBase]]:
    """ 获取 Pydantic 错误编号 - 错误类 映射 """

    err_classes: List[Type] = [
        *PydanticErrorBase.__subclasses__(),
        *PydanticCustomErrorBase.__subclasses__(),
    ]

    return {
        cls.code: cls
        for cls in err_classes
        if (
            isinstance(cls, type)
            and issubclass(cls, PydanticErrorBase)
            and cls is not PydanticCustomErrorBase
        )
    }
